home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / net / readline-2.0mg.tar.gz / readline-2.0mg.tar / readline-2.0mg / signals.c < prev    next >
C/C++ Source or Header  |  1994-11-20  |  8KB  |  307 lines

  1. /* signals.c -- signal handling support for readline. */
  2.  
  3. /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
  4.  
  5.    This file is part of the GNU Readline Library, a library for
  6.    reading lines of text with interactive input and history editing.
  7.  
  8.    The GNU Readline Library is free software; you can redistribute it
  9.    and/or modify it under the terms of the GNU General Public License
  10.    as published by the Free Software Foundation; either version 1, or
  11.    (at your option) any later version.
  12.  
  13.    The GNU Readline Library is distributed in the hope that it will be
  14.    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  15.    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    The GNU General Public License is often shipped with GNU software, and
  19.    is generally kept in a file called COPYING or LICENSE.  If you do not
  20.    have a copy of the license, write to the Free Software Foundation,
  21.    675 Mass Ave, Cambridge, MA 02139, USA. */
  22. #define READLINE_LIBRARY
  23.  
  24. #if defined (HAVE_CONFIG_H)
  25. #  include "config.h"
  26. #endif
  27.  
  28. #include <stdio.h>
  29. #include <sys/types.h>
  30. #include <fcntl.h>
  31. #if !defined (NO_SYS_FILE)
  32. #  include <sys/file.h>
  33. #endif /* !NO_SYS_FILE */
  34. #include <signal.h>
  35.  
  36. #if defined (HAVE_UNISTD_H)
  37. #    include <unistd.h>
  38. #endif    /* HAVE_UNISTD_H */
  39.  
  40. #if defined (HAVE_STDLIB_H)
  41. #  include <stdlib.h>
  42. #else
  43. #  include "ansi_stdlib.h"
  44. #endif /* HAVE_STDLIB_H */
  45.  
  46. #include <errno.h>
  47. /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
  48. #if !defined (errno)
  49. extern int errno;
  50. #endif /* !errno */
  51.  
  52. #include "posixstat.h"
  53. #include "rldefs.h"
  54.  
  55. #if defined (GWINSZ_IN_SYS_IOCTL)
  56. #  include <sys/ioctl.h>
  57. #endif /* GWINSZ_IN_SYS_IOCTL */
  58.  
  59. /* Some standard library routines. */
  60. #include "readline.h"
  61. #include "history.h"
  62.  
  63. static void cr ();
  64.  
  65. extern int readline_echoing_p;
  66. extern int rl_pending_input;
  67. extern char *term_cr;
  68.  
  69. extern int _rl_meta_flag;
  70.  
  71. extern int _rl_output_character_function ();
  72.  
  73. extern void free_undo_list ();
  74.  
  75. #if defined (VOID_SIGHANDLER)
  76. #  define sighandler void
  77. #else
  78. #  define sighandler int
  79. #endif /* VOID_SIGHANDLER */
  80.  
  81. /* This typedef is equivalant to the one for Function; it allows us
  82.    to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */
  83. typedef sighandler SigHandler ();
  84.  
  85. #if defined (__GO32__)
  86. #  undef HANDLE_SIGNALS
  87. #endif /* __GO32__ */
  88.  
  89. #if defined (STATIC_MALLOC)
  90. static char *xmalloc (), *xrealloc ();
  91. #else
  92. extern char *xmalloc (), *xrealloc ();
  93. #endif /* STATIC_MALLOC */
  94.  
  95.  
  96. /* **************************************************************** */
  97. /*                                        */
  98. /*               Signal Handling                          */
  99. /*                                    */
  100. /* **************************************************************** */
  101.  
  102. #if defined (SIGWINCH)
  103. static SigHandler *old_sigwinch = (SigHandler *)NULL;
  104.  
  105. static sighandler
  106. rl_handle_sigwinch (sig)
  107.      int sig;
  108. {
  109.   if (readline_echoing_p)
  110.     {
  111.       _rl_set_screen_size (fileno (rl_instream), 1);
  112.  
  113.       cr ();                /* was crlf () */
  114.       rl_forced_update_display ();
  115.     }
  116.  
  117.   if (old_sigwinch &&
  118.       old_sigwinch != (SigHandler *)SIG_IGN &&
  119.       old_sigwinch != (SigHandler *)SIG_DFL)
  120.     (*old_sigwinch) (sig);
  121. #if !defined (VOID_SIGHANDLER)
  122.   return (0);
  123. #endif /* VOID_SIGHANDLER */
  124. }
  125. #endif  /* SIGWINCH */
  126.  
  127. #if defined (HANDLE_SIGNALS)
  128. /* Interrupt handling. */
  129. static SigHandler
  130.   *old_int  = (SigHandler *)NULL,
  131.   *old_alrm = (SigHandler *)NULL;
  132. #if !defined (SHELL)
  133. static SigHandler
  134.   *old_tstp = (SigHandler *)NULL,
  135.   *old_ttou = (SigHandler *)NULL,
  136.   *old_ttin = (SigHandler *)NULL,
  137.   *old_cont = (SigHandler *)NULL;
  138. #endif /* !SHELL */
  139.  
  140. /* Handle an interrupt character. */
  141. static sighandler
  142. rl_signal_handler (sig)
  143.      int sig;
  144. {
  145. #if defined (HAVE_POSIX_SIGNALS)
  146.   sigset_t set;
  147. #else /* !HAVE_POSIX_SIGNALS */
  148. #  if defined (HAVE_BSD_SIGNALS)
  149.   long omask;
  150. #  endif /* HAVE_BSD_SIGNALS */
  151. #endif /* !HAVE_POSIX_SIGNALS */
  152.  
  153. #if !defined (HAVE_BSD_SIGNALS) && !defined (HAVE_POSIX_SIGNALS)
  154.   /* Since the signal will not be blocked while we are in the signal
  155.      handler, ignore it until rl_clear_signals resets the catcher. */
  156.   if (sig == SIGINT)
  157.     signal (sig, SIG_IGN);
  158. #endif /* !HAVE_BSD_SIGNALS */
  159.  
  160.   switch (sig)
  161.     {
  162.     case SIGINT:
  163.       {
  164.     register HIST_ENTRY *entry;
  165.  
  166.     free_undo_list ();
  167.  
  168.     entry = current_history ();
  169.     if (entry)
  170.       entry->data = (char *)NULL;
  171.       }
  172.       _rl_kill_kbd_macro ();
  173.       rl_clear_message ();
  174.       rl_init_argument ();
  175.  
  176. #if defined (SIGTSTP)
  177.     case SIGTSTP:
  178.     case SIGTTOU:
  179.     case SIGTTIN:
  180. #endif /* SIGTSTP */
  181.     case SIGALRM:
  182.       rl_clean_up_for_exit ();
  183.       rl_deprep_terminal ();
  184.       rl_clear_signals ();
  185.       rl_pending_input = 0;
  186.  
  187. #if defined (HAVE_POSIX_SIGNALS)
  188.       sigprocmask (SIG_BLOCK, (sigset_t *)NULL, &set);
  189.       sigdelset (&set, sig);
  190. #else /* !HAVE_POSIX_SIGNALS */
  191. #  if defined (HAVE_BSD_SIGNALS)
  192.       omask = sigblock (0);
  193. #  endif /* HAVE_BSD_SIGNALS */
  194. #endif /* !HAVE_POSIX_SIGNALS */
  195.  
  196.       kill (getpid (), sig);
  197.  
  198.       /* Let the signal that we just sent through.  */
  199. #if defined (HAVE_POSIX_SIGNALS)
  200.       sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL);
  201. #else /* !HAVE_POSIX_SIGNALS */
  202. #  if defined (HAVE_BSD_SIGNALS)
  203. "choke!!!"
  204.       sigsetmask (omask & ~(sigmask (sig)));
  205. #  endif /* HAVE_BSD_SIGNALS */
  206. #endif /* !HAVE_POSIX_SIGNALS */
  207.  
  208.       rl_prep_terminal (_rl_meta_flag);
  209.       rl_set_signals ();
  210.     }
  211.  
  212. #if !defined (VOID_SIGHANDLER)
  213.   return (0);
  214. #endif /* !VOID_SIGHANDLER */
  215. }
  216.  
  217. #if defined (HAVE_POSIX_SIGNALS)
  218. static SigHandler *
  219. rl_set_sighandler (sig, handler)
  220.      int sig;
  221.      SigHandler *handler;
  222. {
  223.   struct sigaction act, oact;
  224.  
  225.   act.sa_handler = handler;
  226.   act.sa_flags = 0;
  227.   sigemptyset (&act.sa_mask);
  228.   sigemptyset (&oact.sa_mask);
  229.   sigaction (sig, &act, &oact);
  230.   return (oact.sa_handler);
  231. }
  232.  
  233. #else /* !HAVE_POSIX_SIGNALS */
  234. #  define rl_set_sighandler(sig, handler) (SigHandler *)signal (sig, handler)
  235. #endif /* !HAVE_POSIX_SIGNALS */
  236.  
  237. rl_set_signals ()
  238. {
  239.   old_int = (SigHandler *)rl_set_sighandler (SIGINT, rl_signal_handler);
  240.   if (old_int == (SigHandler *)SIG_IGN)
  241.     signal (SIGINT, SIG_IGN);
  242.  
  243.   old_alrm = (SigHandler *)rl_set_sighandler (SIGALRM, rl_signal_handler);
  244.   if (old_alrm == (SigHandler *)SIG_IGN)
  245.     signal (SIGALRM, SIG_IGN);
  246.  
  247. #if !defined (SHELL)
  248.  
  249. #if defined (SIGTSTP)
  250.   old_tstp = (SigHandler *)rl_set_sighandler (SIGTSTP, rl_signal_handler);
  251.   if (old_tstp == (SigHandler *)SIG_IGN)
  252.     signal (SIGTSTP, SIG_IGN);
  253. #endif /* SIGTSTP */
  254. #if defined (SIGTTOU)
  255.   old_ttou = (SigHandler *)rl_set_sighandler (SIGTTOU, rl_signal_handler);
  256.   old_ttin = (SigHandler *)rl_set_sighandler (SIGTTIN, rl_signal_handler);
  257.  
  258.   if (old_tstp == (SigHandler *)SIG_IGN)
  259.     {
  260.       signal (SIGTTOU, SIG_IGN);
  261.       signal (SIGTTIN, SIG_IGN);
  262.     }
  263. #endif /* SIGTTOU */
  264.  
  265. #endif /* !SHELL */
  266.  
  267. #if defined (SIGWINCH)
  268.   old_sigwinch =
  269.     (SigHandler *) rl_set_sighandler (SIGWINCH, rl_handle_sigwinch);
  270. #endif /* SIGWINCH */
  271.   return 0;
  272. }
  273.  
  274. rl_clear_signals ()
  275. {
  276.   rl_set_sighandler (SIGINT, old_int);
  277.   rl_set_sighandler (SIGALRM, old_alrm);
  278.  
  279. #if !defined (SHELL)
  280.  
  281. #if defined (SIGTSTP)
  282.   signal (SIGTSTP, old_tstp);
  283. #endif
  284.  
  285. #if defined (SIGTTOU)
  286.   signal (SIGTTOU, old_ttou);
  287.   signal (SIGTTIN, old_ttin);
  288. #endif /* SIGTTOU */
  289.  
  290. #endif /* !SHELL */
  291.  
  292. #if defined (SIGWINCH)
  293.   signal (SIGWINCH, old_sigwinch);
  294. #endif
  295.  
  296.   return 0;
  297. }
  298.  
  299. /* Move to the start of the current line. */
  300. static void
  301. cr ()
  302. {
  303.   if (term_cr)    
  304.     tputs (term_cr, 1, _rl_output_character_function);
  305. }
  306. #endif  /* HANDLE_SIGNALS */
  307.